home *** CD-ROM | disk | FTP | other *** search
/ Mastering Microsoft Visual Basic 5 / Mastering Microsoft Visual Basic 5.ISO / demo code / ch11 / wininet / frmmain.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-01-08  |  14.9 KB  |  500 lines

  1. VERSION 5.00
  2. Object = "{48E59290-9880-11CF-9754-00AA00C00908}#1.0#0"; "MSINET.OCX"
  3. Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.1#0"; "COMCTL32.OCX"
  4. Begin VB.Form frmMain 
  5.    BorderStyle     =   3  'Fixed Dialog
  6.    ClientHeight    =   4800
  7.    ClientLeft      =   2820
  8.    ClientTop       =   1935
  9.    ClientWidth     =   6795
  10.    LinkTopic       =   "Form1"
  11.    LockControls    =   -1  'True
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   4800
  15.    ScaleWidth      =   6795
  16.    ShowInTaskbar   =   0   'False
  17.    Begin VB.CommandButton cmdUser 
  18.       Caption         =   "Set User"
  19.       Height          =   435
  20.       Left            =   135
  21.       TabIndex        =   18
  22.       Top             =   2400
  23.       Width           =   1095
  24.    End
  25.    Begin VB.CommandButton cmdCancel 
  26.       Cancel          =   -1  'True
  27.       Caption         =   "&Cancel"
  28.       Height          =   435
  29.       Left            =   4095
  30.       TabIndex        =   21
  31.       Top             =   2400
  32.       Width           =   1095
  33.    End
  34.    Begin VB.CommandButton cmdClose 
  35.       Caption         =   "&Close"
  36.       Height          =   435
  37.       Left            =   5415
  38.       TabIndex        =   22
  39.       Top             =   2400
  40.       Width           =   1095
  41.    End
  42.    Begin VB.ComboBox cboOperation 
  43.       Height          =   315
  44.       Left            =   1185
  45.       TabIndex        =   13
  46.       Top             =   1950
  47.       Width           =   2115
  48.    End
  49.    Begin VB.TextBox txtRequestHeaders 
  50.       Height          =   345
  51.       Left            =   4575
  52.       TabIndex        =   17
  53.       Top             =   1950
  54.       Width           =   2115
  55.    End
  56.    Begin VB.TextBox txtData 
  57.       Height          =   345
  58.       Left            =   4575
  59.       TabIndex        =   15
  60.       Top             =   1500
  61.       Width           =   2115
  62.    End
  63.    Begin VB.TextBox txtURL 
  64.       Height          =   345
  65.       Left            =   1185
  66.       TabIndex        =   11
  67.       Top             =   1500
  68.       Width           =   2115
  69.    End
  70.    Begin VB.CommandButton cmdOpenURL 
  71.       Caption         =   "&OpenURL"
  72.       Height          =   435
  73.       Left            =   1455
  74.       TabIndex        =   19
  75.       Top             =   2400
  76.       Width           =   1095
  77.    End
  78.    Begin VB.CommandButton cmdExecute 
  79.       Caption         =   "E&xecute"
  80.       Height          =   435
  81.       Left            =   2775
  82.       TabIndex        =   20
  83.       Top             =   2400
  84.       Width           =   1095
  85.    End
  86.    Begin VB.TextBox txtOutput 
  87.       Height          =   1500
  88.       Left            =   105
  89.       MultiLine       =   -1  'True
  90.       ScrollBars      =   2  'Vertical
  91.       TabIndex        =   23
  92.       Top             =   2970
  93.       Width           =   6585
  94.    End
  95.    Begin VB.Frame Frame2 
  96.       Caption         =   "Proxy Ty&pe"
  97.       Height          =   675
  98.       Left            =   3510
  99.       TabIndex        =   4
  100.       Top             =   90
  101.       Width           =   3165
  102.       Begin VB.OptionButton optProxy 
  103.          Caption         =   "&Default"
  104.          Height          =   315
  105.          Index           =   0
  106.          Left            =   120
  107.          TabIndex        =   5
  108.          Top             =   270
  109.          Value           =   -1  'True
  110.          Width           =   900
  111.       End
  112.       Begin VB.OptionButton optProxy 
  113.          Caption         =   "Di&rect"
  114.          Height          =   315
  115.          Index           =   1
  116.          Left            =   1200
  117.          TabIndex        =   6
  118.          Top             =   270
  119.          Width           =   900
  120.       End
  121.       Begin VB.OptionButton optProxy 
  122.          Caption         =   "&Named"
  123.          Height          =   315
  124.          Index           =   2
  125.          Left            =   2200
  126.          TabIndex        =   7
  127.          Top             =   270
  128.          Width           =   900
  129.       End
  130.    End
  131.    Begin VB.TextBox txtProxyServer 
  132.       Enabled         =   0   'False
  133.       Height          =   345
  134.       Left            =   4590
  135.       TabIndex        =   9
  136.       Top             =   900
  137.       Width           =   2085
  138.    End
  139.    Begin InetCtlsObjects.Inet Inet1 
  140.       Left            =   7170
  141.       Top             =   2160
  142.       _ExtentX        =   1005
  143.       _ExtentY        =   1005
  144.    End
  145.    Begin ComctlLib.StatusBar sbMain 
  146.       Align           =   2  'Align Bottom
  147.       Height          =   285
  148.       Left            =   0
  149.       TabIndex        =   24
  150.       Top             =   4515
  151.       Width           =   6795
  152.       _ExtentX        =   11986
  153.       _ExtentY        =   503
  154.       SimpleText      =   ""
  155.       _Version        =   327680
  156.       BeginProperty Panels {0713E89E-850A-101B-AFC0-4210102A8DA7} 
  157.          NumPanels       =   2
  158.          BeginProperty Panel1 {0713E89F-850A-101B-AFC0-4210102A8DA7} 
  159.             AutoSize        =   2
  160.             TextSave        =   ""
  161.             Key             =   ""
  162.             Object.Tag             =   ""
  163.          EndProperty
  164.          BeginProperty Panel2 {0713E89F-850A-101B-AFC0-4210102A8DA7} 
  165.             Alignment       =   2
  166.             AutoSize        =   1
  167.             Object.Width           =   9366
  168.             TextSave        =   ""
  169.             Key             =   ""
  170.             Object.Tag             =   ""
  171.          EndProperty
  172.       EndProperty
  173.       BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
  174.          Name            =   "MS Sans Serif"
  175.          Size            =   8.25
  176.          Charset         =   0
  177.          Weight          =   400
  178.          Underline       =   0   'False
  179.          Italic          =   0   'False
  180.          Strikethrough   =   0   'False
  181.       EndProperty
  182.       MouseIcon       =   "frmMain.frx":0000
  183.    End
  184.    Begin VB.Label Label9 
  185.       Caption         =   "Req.&Headers:"
  186.       Height          =   255
  187.       Left            =   3525
  188.       TabIndex        =   16
  189.       Top             =   1980
  190.       Width           =   975
  191.    End
  192.    Begin VB.Label Label8 
  193.       Caption         =   "&Data:"
  194.       Height          =   255
  195.       Left            =   3525
  196.       TabIndex        =   14
  197.       Top             =   1530
  198.       Width           =   975
  199.    End
  200.    Begin VB.Label lblRemotePort 
  201.       BorderStyle     =   1  'Fixed Single
  202.       Height          =   345
  203.       Left            =   1170
  204.       TabIndex        =   3
  205.       Top             =   600
  206.       Width           =   2085
  207.    End
  208.    Begin VB.Label Label7 
  209.       Caption         =   "Remote Port:"
  210.       Height          =   255
  211.       Left            =   90
  212.       TabIndex        =   2
  213.       Top             =   630
  214.       Width           =   975
  215.    End
  216.    Begin VB.Label lblRemoteHost 
  217.       BorderStyle     =   1  'Fixed Single
  218.       Height          =   345
  219.       Left            =   1170
  220.       TabIndex        =   1
  221.       Top             =   90
  222.       Width           =   2085
  223.    End
  224.    Begin VB.Label Label6 
  225.       Caption         =   "&URL:"
  226.       Height          =   255
  227.       Left            =   135
  228.       TabIndex        =   10
  229.       Top             =   1530
  230.       Width           =   975
  231.    End
  232.    Begin VB.Label Label5 
  233.       Caption         =   "&Execute Op:"
  234.       Height          =   255
  235.       Left            =   135
  236.       TabIndex        =   12
  237.       Top             =   1980
  238.       Width           =   975
  239.    End
  240.    Begin VB.Line Line2 
  241.       BorderWidth     =   2
  242.       X1              =   15
  243.       X2              =   6945
  244.       Y1              =   1350
  245.       Y2              =   1350
  246.    End
  247.    Begin VB.Label Label4 
  248.       Caption         =   "Proxy &Server:"
  249.       Height          =   255
  250.       Left            =   3510
  251.       TabIndex        =   8
  252.       Top             =   930
  253.       Width           =   975
  254.    End
  255.    Begin VB.Label Label1 
  256.       Caption         =   "Remote Host:"
  257.       Height          =   255
  258.       Left            =   90
  259.       TabIndex        =   0
  260.       Top             =   120
  261.       Width           =   975
  262.    End
  263. Attribute VB_Name = "frmMain"
  264. Attribute VB_GlobalNameSpace = False
  265. Attribute VB_Creatable = False
  266. Attribute VB_PredeclaredId = True
  267. Attribute VB_Exposed = False
  268. Option Explicit
  269. Enum Protocol
  270.   ptlNONE
  271.   ptlFTP
  272.   ptlHTTP
  273. End Enum
  274. Dim ptl As Protocol
  275. Dim lAccessType As Long
  276. Dim bResponseCompleted As Boolean
  277. Dim strStateChange(0 To 12) As String
  278. Private Function SetProxyProperties() As Boolean
  279.   SetProxyProperties = True
  280.   With Inet1
  281.     ' Set Proxy Properties
  282.     If lAccessType = icNamedProxy Then
  283.       If Len(txtProxyServer) = 0 Then
  284.         MsgBox "Enter proxy server"
  285.         txtProxyServer.SetFocus
  286.         SetProxyProperties = False
  287.         Exit Function
  288.       End If
  289.       .Proxy = txtProxyServer
  290.     End If
  291.     .AccessType = lAccessType
  292.   End With
  293. End Function
  294. Private Sub cmdCancel_Click()
  295.   ' Cancel the current operation
  296.   If Inet1.StillExecuting Then Inet1.Cancel
  297.   cmdOpenURL.Enabled = True
  298.   cmdExecute.Enabled = True
  299. End Sub
  300. Private Sub cmdClose_Click()
  301.   If Inet1.StillExecuting Then Inet1.Cancel
  302.   Unload Me
  303. End Sub
  304. Private Sub cmdExecute_Click()
  305.   On Error GoTo Errhandler
  306.   If Not ValidOperation Then
  307.     cboOperation.SetFocus
  308.     Exit Sub
  309.   End If
  310.   SetProxyProperties
  311.   DisplayConnectInfo
  312.   Inet1.URL = txtURL
  313.   bResponseCompleted = False
  314.   txtOutput = ""
  315.   ' If the protocol is FTP, execute with the choosen operation.
  316.   If ptl = ptlFTP Then
  317.     If Len(cboOperation.Text) = 0 Then
  318.       MsgBox "You must choose an FTP operation"
  319.       cboOperation.SetFocus
  320.       Exit Sub
  321.     End If
  322.     Inet1.Execute , "" & cboOperation & ""
  323.   ElseIf ptl = ptlHTTP Then
  324.     If Len(cboOperation.Text) = 0 Then
  325.       MsgBox "You must choose an HTTP operation"
  326.       cboOperation.SetFocus
  327.       Exit Sub
  328.     End If
  329.     Inet1.Execute txtURL, "" & cboOperation & "" _
  330.       , "" & txtData & "", "" & txtRequestHeaders & ""
  331.   Else
  332.     MsgBox "Invalid Protocol Type. Preface your URL with" & _
  333.       " 'ftp://' or 'http://'"
  334.     txtURL.SetFocus
  335.     Exit Sub
  336.   End If
  337.   cmdOpenURL.Enabled = False
  338.   cmdExecute.Enabled = False
  339.   While Inet1.StillExecuting
  340.     DoEvents
  341.   Wend
  342.   cmdOpenURL.Enabled = True
  343.   cmdExecute.Enabled = True
  344.   Exit Sub
  345. Errhandler:
  346.   MsgBox "Error on Execute" & vbCrLf & Err.Description
  347.   cmdOpenURL.Enabled = True
  348.   cmdExecute.Enabled = True
  349. End Sub
  350. Private Sub Inet1_StateChanged(ByVal State As Integer)
  351.   DisplayStateStatus (State)
  352.   Debug.Print "State Change: " & strStateChange(State)
  353.   Select Case State
  354.     Case icResponseCompleted
  355.       Dim s As String
  356.       ' Get the first chunk.
  357.       s = Inet1.GetChunk(1024)
  358.       
  359.       Do While Len(s) > 0
  360.         txtOutput = txtOutput & s
  361.         s = Inet1.GetChunk(1024)
  362.       Loop
  363.       bResponseCompleted = True
  364.     Case icError
  365.       Debug.Print "An FTP Error Occurred"
  366.       MsgBox "No.: " & Inet1.ResponseCode & vbCrLf & _
  367.         "Desc.: " & Inet1.ResponseInfo, , "Execute Error"
  368.   End Select
  369. End Sub
  370. Private Sub cmdOpenURL_Click()
  371.   On Error GoTo Errhandler
  372.   SetProxyProperties
  373.   ' If Len(txtURL) Then Inet1.URL = txtURL
  374.   txtOutput = Inet1.OpenURL("" & txtURL & "")
  375.   DisplayConnectInfo
  376.   Exit Sub
  377. Errhandler:
  378.   MsgBox "Error on Execute" & vbCrLf & Err.Description
  379. End Sub
  380. Private Sub cmdUser_Click()
  381.   ' Display the User logon form
  382.   frmLogin.Show 1, Me
  383. End Sub
  384. Private Sub Form_Load()
  385.   lAccessType = icUseDefault
  386.   InitStateChangeString
  387.   ptl = ptlNONE
  388. End Sub
  389. Private Sub optProxy_Click(Index As Integer)
  390.   ' Store the current proxy selection
  391.   lAccessType = Index
  392.   ' Disable the proxy server textbox is not a named proxy
  393.   If Index = icNamedProxy Then
  394.     txtProxyServer.Enabled = True
  395.     txtProxyServer.SetFocus
  396.   Else
  397.     txtProxyServer.Enabled = False
  398.   End If
  399. End Sub
  400. Private Sub txtURL_Change()
  401.   cmdOpenURL.Enabled = Len(txtURL)
  402.   If UCase$(Left$(txtURL, 6)) = "FTP://" And ptl <> ptlFTP Then
  403.     ptl = ptlFTP
  404.     FillFTPOperations
  405.     cboOperation.ListIndex = 1
  406.   ElseIf UCase$(Left$(txtURL, 7)) = "HTTP://" And ptl <> ptlHTTP Then
  407.     ptl = ptlHTTP
  408.     FillHTTPOperations
  409.     cboOperation.ListIndex = 1
  410.   ElseIf UCase$(Left$(txtURL, 6)) <> "FTP://" And _
  411.       UCase$(Left$(txtURL, 7)) <> "HTTP://" Then
  412.     ptl = ptlNONE
  413.     cboOperation.Clear
  414.   End If
  415. End Sub
  416. Private Sub InitStateChangeString()
  417.   strStateChange(0) = ""
  418.   strStateChange(1) = "Resolving Host..."
  419.   strStateChange(2) = "Host Resolved"
  420.   strStateChange(3) = "Connecting..."
  421.   strStateChange(4) = "Connected"
  422.   strStateChange(5) = "Requesting..."
  423.   strStateChange(6) = "Request Sent"
  424.   strStateChange(7) = "Receiving Response..."
  425.   strStateChange(8) = "Response Received"
  426.   strStateChange(9) = "Disconnecting..."
  427.   strStateChange(10) = "Disconnected"
  428.   strStateChange(11) = "Error"
  429.   strStateChange(12) = "Response Completed"
  430. End Sub
  431. Private Sub DisplayStateStatus(ByVal State As Integer)
  432.   Debug.Assert State >= 0 And State < 13
  433.   sbMain.Panels(1).Text = strStateChange(State)
  434. End Sub
  435. Private Sub FillFTPOperations()
  436.   cboOperation.Clear
  437.   cboOperation.AddItem "CD <file1>"
  438.   cboOperation.AddItem "CDUP "
  439.   cboOperation.AddItem "DELETE <file1>"
  440.   cboOperation.AddItem "DIR [file1]"
  441.   cboOperation.AddItem "GET <file1> <file2>"
  442.   cboOperation.AddItem "MKDIR <dir1>"
  443.   cboOperation.AddItem "PUT <file1> <file2>"
  444.   cboOperation.AddItem "PWD "
  445.   cboOperation.AddItem "QUIT "
  446.   cboOperation.AddItem "RENAME <file1> <file2>"
  447.   cboOperation.AddItem "RMDIR <dir1>"
  448.   cboOperation.AddItem "SIZE <file1>"
  449. End Sub
  450. Private Sub FillHTTPOperations()
  451.   cboOperation.Clear
  452.   cboOperation.AddItem "GET"
  453.   cboOperation.AddItem "HEAD"
  454.   cboOperation.AddItem "POST"
  455.   cboOperation.AddItem "PUT"
  456. End Sub
  457. Private Function ValidOperation() As Boolean
  458.   Dim posStart As Integer
  459.   Dim pos As Integer
  460.   ValidOperation = True
  461.   ' Search for mandatory parameters not set
  462.   posStart = 1
  463.   pos = InStr(posStart, cboOperation.Text, "<", vbTextCompare)
  464.   If pos Then
  465.     cboOperation.SelStart = pos - 1
  466.     posStart = pos + 1
  467.     pos = InStr(posStart, cboOperation, ">", vbTextCompare)
  468.     If pos > 0 Then
  469.       ValidOperation = False
  470.       cboOperation.SelLength = pos - cboOperation.SelStart + 1
  471.     Else
  472.       cboOperation.SelLength = 999
  473.     End If
  474.     ValidOperation = False
  475.     Exit Function
  476.   End If
  477.   ' Check for any optional parameters
  478.   posStart = 1
  479.   pos = InStr(posStart, cboOperation, "[", vbTextCompare)
  480.   While pos
  481.     If pos Then
  482.       cboOperation.SelStart = pos - 1
  483.       posStart = pos + 1
  484.       pos = InStr(posStart, cboOperation, "]", vbTextCompare)
  485.       If pos Then
  486.         cboOperation.SelLength = pos - cboOperation.SelStart + 1
  487.         cboOperation.SelText = ""
  488.       Else
  489.         Exit Function
  490.       End If
  491.     End If
  492.     posStart = 1
  493.     pos = InStr(posStart, cboOperation, "[")
  494.   Wend
  495. End Function
  496. Private Sub DisplayConnectInfo()
  497.   lblRemoteHost = Inet1.RemoteHost
  498.   lblRemotePort = Inet1.RemotePort
  499. End Sub
  500.